from panorama import Panaroma  # 导入全景图拼接库
import imutils  # 导入图像处理库
import cv2  # 导入OpenCV库


no_of_images = int(input("Enter the number of images you want to concatenate: "))  # 输入要拼接的图片数量
print("Enter the image names with extension in order of left to right in the way you want to concatenate: ")
# like tajm1.jpg, tajm2.jpg, tajm3.jpg .... tajmn.jpg

# 输入每张图片的路径和文件名，并将其存储在列表中
filename = []
for i in range(no_of_images):
    filename.append(input("Enter the %d image name along with path and extension: " % (i + 1)))

images = []  # 存储读取的图片
# 读取每张图片并将其存储在列表中
for i in range(no_of_images):
    images.append(cv2.imread(filename[i]))

# We need to modify the images width and height to keep our aspect ratio same across images
# 调整每张图片的宽度和高度，以保持宽高比一致
for i in range(no_of_images):
    images[i] = imutils.resize(images[i], width=400)

for i in range(no_of_images):
    images[i] = imutils.resize(images[i], height=400)

# 创建全景图对象
panorama = Panaroma()
# 拼接图片并生成全景图
if no_of_images == 2:
    (result, matched_points) = panorama.image_stitch([images[0], images[1]], match_status=True)
else:
    (result, matched_points) = panorama.image_stitch([images[no_of_images - 2], images[no_of_images - 1]], match_status=True)
    for i in range(no_of_images - 2):
        (result, matched_points) = panorama.image_stitch([images[no_of_images - i - 3], result], match_status=True)

# show input images
# for i in range(no_of_images):
#     cv2.imshow("Image {k}".format(k=i + 1), images[i])

# show the panorama image and valid matched points
# 显示全景图
cv2.imshow("Keypoint Matches", matched_points)
cv2.imshow("Panorama", result)

# save panorama and matched_points images in output folder
cv2.imwrite("output/matched_points.jpg", matched_points)
cv2.imwrite("output/panorama_image.jpg", result)

cv2.waitKey(0)
cv2.destroyAllWindows()
